package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.dao.WmsProvinceRankingReportMapper;
import com.jy.wms.pojo.DirectSellingprovinces;
import com.jy.wms.pojo.WmsProvinceRankingReport;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName: WmsProvinceRankingReportService.java
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author lqc
 * @Date Thu Mar 22 18:05:53 CST 2018
 */

@Service
public class WmsProvinceRankingReportService extends BaseService<WmsProvinceRankingReportMapper, WmsProvinceRankingReport> {


    /**
     * 直销省份排名报表查询（根据时间查询）
     *
     * @return JSONObject
     */

    public JSONObject baseselectbyprimarykey(DirectSellingprovinces mapp, String hzid, String ckid) {
        JSONObject allJson = new JSONObject();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (mapp.getFpsj_start()!=0 && mapp.getFpsj_end()!=0){
                long start = mapp.getFpsj_start();
                long End= mapp.getFpsj_end();
                Calendar c = Calendar.getInstance();
                c.setTimeInMillis(start);
                Date date = c.getTime();
                String start_time=sdf.format(date);
                c.setTimeInMillis(End);
                Date date2 = c.getTime();
                String End_time=sdf.format(date2);
                WmsProvinceRankingReport ppr=  new WmsProvinceRankingReport();
                //查询直销省份排名报表
                PageHelper.startPage(mapp.getPage(), mapp.getRows());
                Page<WmsProvinceRankingReport> Sales = (Page<WmsProvinceRankingReport>) dao.baseselectbyprimarykey(start_time, End_time, hzid, ckid);

                int i = 0;//记录排名
                int sddl = 0;//记录订单量
                int  ddl=0;//订单量总计
                Double xse=0.0;//销售额总计
                Double xsezj=0.0;//销售额百分比总计
                Double ddlzj=0.0;//订单量百分比总计
                for (WmsProvinceRankingReport user : Sales) {
                    //订单量排序
                    if (sddl == 0) {
                        sddl = Integer.parseInt(user.getDdl());
                        i++;
                    } else {
                        if (Integer.parseInt(user.getDdl()) == sddl) {
                        } else {
                            sddl = Integer.parseInt(user.getDdl());
                            i++;
                        }
                    }
                    user.setDdlpm(i);

                    //计算所有省的订单量
                    int ddl1= Integer.parseInt(user.getDdl());
                    ddl+=ddl1;
                    ppr.setDdl(ddl+"");
                    //计算所有省的销售额
                    if (!user.getXse().equals("") && !user.equals(null)){
                        //Double xse1=Double.parseDouble(user.getXse());
                        //xse+=xse1;
                        xse+=user.getXse();
                        ppr.setXse(xse);
                    }else if (!user.getXse().equals("") && !user.equals(null)){

                        Double xse1= Double.parseDouble("0.0");
                        xse+=xse1;
                        ppr.setXse(xse);
                    }

                }

                // 创建一个数值格式化对象
                NumberFormat numberFormat = NumberFormat.getInstance();
                //计算百分比
                for (WmsProvinceRankingReport user : Sales) {
                    //订单量百分比
                    int dd= Integer.parseInt(user.getDdl());
                    if(ddl==0){
                        user.setDdlbfb(0+"%");
                    }else{
                        String ddbfb = numberFormat.format((float) dd / (float) ddl * 100);
                        user.setDdlbfb(ddbfb+"%");
                        //订单量百分比总计
                        Double ddzj= Double.parseDouble(ddbfb);
                        ddlzj+=ddzj;
                        BigDecimal bg = new BigDecimal(ddlzj);
                        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                        ppr.setDdlbfb(f1+"%");
                    }
                    //销售额百分比
                    //Double xs=Double.parseDouble(user.getXse());
                    if (xse==0.0){
                        user.setXsezb(0.00+"%");
                    }else{
                        BigDecimal b = new BigDecimal(user.getXse() / xse * 100);
                        Double xx = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                        user.setXsezb(xx+"%");
                        //计算销售额百分比总计
                        xsezj+=xx;
                        BigDecimal bg = new BigDecimal(xsezj);
                        double x = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                        ppr.setXsezb(x+"%");
                    }
                }
                ppr.setSheng("合计");
                Sales.add(ppr);
                JSONArray array = JSONArray.fromObject(Sales);
                allJson = SetStatus.setSearchType(array.toString(), (int) Sales.getTotal());
                allJson.put("contentList", Sales);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return allJson;
    }
}
